/*
记录本日志收集程序产生的相关日志信息
*/
package mainfuncs

import (
	"encoding/json"
	"fmt"

	"github.com/astaxie/beego/logs"
)


//InitLogger 根据读取的配置信息appConfig初始化日志库,功能：记录产生此系统运行时的相关日志
func InitLogger() (err error) {

	config := make(map[string]interface{})
	config["filename"] = AppConfig.LogPath
	config["level"] = convertLogLevel(AppConfig.LogLevel)

	configStr, err := json.Marshal(config)
	if err != nil {
		fmt.Println("initLogger failed, marshal err:", err)
		return
	}

	logs.SetLogger(logs.AdapterFile, string(configStr))
	return
}

//把appConfig.logLevel字符串转换成对应的int类型
func convertLogLevel(level string) int {

	switch level {
	case "debug":
		return logs.LevelDebug
	case "warn":
		return logs.LevelWarn
	case "info":
		return logs.LevelInfo
	case "trace":
		return logs.LevelTrace
	}

	return logs.LevelDebug
}